﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.ZX.Manager
{
	/// <summary>
	/// 消息分发管理器
	/// </summary>
	public class MsgManager:BaseSingleton<MsgManager>
	{
        /// <summary>
        /// 监听字典
        /// </summary>
		private Dictionary<string, List<MessageEvent>> dictMsgEvent = null;

		public MsgManager():base()
		{

		}
		public override void onInit()
		{
			dictMsgEvent = new Dictionary<string, List<MessageEvent>>();
		}
		/// <summary>
		/// 添加事件监听
		/// </summary>
		/// <param name="msgType"></param>
		/// <param name="msgEvent"></param>
		public void AddListener(MessageType msgType, MessageEvent msgEvent)
		{
			AddListener(msgType.ToString(), msgEvent);
		}
		/// <summary>
		/// 移除事件监听
		/// </summary>
		/// <param name="msgType"></param>
		/// <param name="msgEvent"></param>
		public void RemoveListener(MessageType msgType, MessageEvent msgEvent)
		{
			RemoveListener(msgType.ToString(), msgEvent);
		}

		public void AddListener(string msgName,MessageEvent msgEvent)
		{
			List<MessageEvent> list = new List<MessageEvent>();
            if (dictMsgEvent.ContainsKey(msgName))
            {
                list = dictMsgEvent[msgName];
                list.Add(msgEvent);
            }
            else
            {
                list.Add(msgEvent);
                dictMsgEvent.Add(msgName, list);
            }
          
        }

		public void RemoveListener(string msgName,MessageEvent msgEvent)
		{
			if (dictMsgEvent.ContainsKey(msgName))
			{
				List<MessageEvent> list = dictMsgEvent[msgName];
				if (list.Contains(msgEvent))
					list.Remove(msgEvent);
				if (list.Count <= 0)
					dictMsgEvent.Remove(msgName);
					
			}
		}
		public void RemoveAllListener()
		{
			dictMsgEvent.Clear();
		}

		public void SendMessage(Message msg)
		{
			DoSendDispatcher(msg);
		}
		public void SendMessage(string name,object sender)
		{
			SendMessage(new Message(name, sender));
		}
		public void SendMessage(string name, object sender,object content)
		{
			SendMessage(new Message( name, sender, content));
		}
        public void SendMessage(MessageType msgType, object sender, object content, params object[] msgParams)
        {
            SendMessage(msgType.ToString(), sender, content, msgParams);
        }
        public void SendMessage(string name, object sender, object content,params object[] msgParams)
		{
			SendMessage(new Message(name, sender, content, msgParams));
		}

		/// <summary>
		/// 消息分发
		/// </summary>
		/// <param name="msg"></param>
		private void DoSendDispatcher(Message msg)
		{
            CustomDebug.Log(msg.Name);
			if (dictMsgEvent == null && !dictMsgEvent.ContainsKey(msg.Name))
				return;
			List<MessageEvent> eventList = dictMsgEvent[msg.Name];
			for (int i = 0; i < eventList.Count; i++)
			{
				MessageEvent msgEvent = eventList[i];
				if (msgEvent != null)
					msgEvent(msg);
			}

			// do something
		}
	}
}
